package com.sbio.udiapp.ui;

import android.os.Bundle;
import android.text.TextUtils;

import androidx.appcompat.app.AppCompatActivity;

import com.sbio.udiapp.databinding.ActivityUnOutStockBinding;
import com.sbio.udiapp.net.api.ApiService;
import com.sbio.udiapp.net.exception.ErrorComsumerSupplier;
import com.sbio.udiapp.net.response.ResponseTransformer;
import com.sbio.udiapp.util.DialogUtil;
import com.sbio.udiapp.util.EventUtil;
import com.sbio.udiapp.util.ToastUtil;
import com.sbio.udiapp.util.ViewUtil;

import javax.inject.Inject;

import dagger.hilt.android.AndroidEntryPoint;

@AndroidEntryPoint
public class UnOutStockActivity extends AppCompatActivity {

    private com.sbio.udiapp.databinding.ActivityUnOutStockBinding binding;

    @Inject
    ApiService apiService;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivityUnOutStockBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());

        binding.outStockNumber.requestFocus();
        binding.outStockNumber.setOnEditorActionListener((v, actionId, event) -> {
            String outStockNo = v.getText().toString();
            if (TextUtils.isEmpty(outStockNo)) {
                return true;
            }
            apiService.checkOutStockCanUnOut(outStockNo)
                    .compose(ResponseTransformer.obtain())
                    .subscribe(ajaxResult -> {

                    }, ErrorComsumerSupplier.of((apiException) -> {
                        DialogUtil.showCommonAlertDialog(apiException.getErrMsg());
                        binding.outStockNumber.setText("");
                        ViewUtil.requestFocus(binding.outStockNumber);
                    }));
            return false;
        });


        //为编码输入框添加回车事件
        binding.etPbNo.setOnEditorActionListener((v, actionId, event) -> {
            String outStockNo = binding.outStockNumber.getText().toString();
            if (TextUtils.isEmpty(outStockNo)) {
                return false;
            }
            if (TextUtils.isEmpty(outStockNo)) {
                DialogUtil.showCommonAlertDialog("请先输入出库单号");
                return false;
            }
            String pbNo = binding.etPbNo.getText().toString();
            if (TextUtils.isEmpty(pbNo)) {
                return false;
            }
            //校验箱码盒码是否存在，是否可以反出库
            apiService.unOutStock(outStockNo,pbNo)
                    .compose(ResponseTransformer.obtain())
                    .subscribe(ajaxResult -> {
                            /*if (ajaxResult.getData(Boolean.class)) {
                                binding.etPbNo.setText("");
                                //获取焦点
                                ViewUtil.requestFocus(binding.etPbNo);
                                return;
                            }*/
                        //校验不通过
                        //清空数据
                        binding.etPbNo.setText("");
                        ViewUtil.requestFocus(binding.etPbNo);
                        ToastUtil.show("反出库成功");
                        //弹出错误框
//                            DialogUtil.showCommonAlertDialog(ajaxResult.getMsg());

                    }, ErrorComsumerSupplier.of((apiException -> {
                        DialogUtil.showCommonAlertDialog(apiException.getErrMsg());
                        binding.etPbNo.setText("");
                        ViewUtil.requestFocus(binding.etPbNo);
                    })));
            return false;
        });
    }
}